package com.sangeng.utils;

import org.apache.poi.ss.formula.functions.T;
import org.springframework.beans.BeanUtils;

import java.util.List;
import java.util.stream.Collectors;

/**
 * ClassName: BeanCopyUtils
 * Package: com.sangeng.utils
 * Description:
 *
 * @Author hjh
 * @Create 2024/8/11 17:35
 * @Version 1.0
 */
public class BeanCopyUtils {
    private BeanCopyUtils() {
    }

    //单个拷贝
    public  static <V> V copyBean(Object source ,Class<V> clazz){
        //创建目标对象
        V result=null;
        try {
            result=clazz.newInstance();
            //实现属性copy
            BeanUtils.copyProperties(source,result);
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
        //返回结果
        return result;
    }

    //列表拷贝
    public static <O,V> List<V> copyBeanList(List<O> list,Class<V> clazz){
        return list.stream().map(o->copyBean(o,clazz)).collect(Collectors.toList());
    }

}
